﻿using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace Cubicle.Excel.Config
{

    [DataContract]
    [Serializable]
    public abstract class BaseDescription
    {
        public BaseDescription() {}

        public BaseDescription(
            string target,
            string methodName,
            //BindMethodEnum bindMethod = BindMethodEnum.BindDirectStatic,
            int methodOffset = 0,
            string functionName = null,
            bool resolveParameters = true,
            bool bindToHandle = false,
            string prefix = "",
            string postfix = "",
            string description = "No description",
            bool isExceptionSafe = false)
        {
            Target = target;
            MethodName = methodName;
            //BindMethod = bindMethod;
            MethodOffset = methodOffset;
            FunctionName = functionName;
            ResolveParameters = resolveParameters;
            BindToHandle = bindToHandle;
            Prefix = prefix;
            PostFix = postfix;
            Description = description;
            IsExceptionSafe = isExceptionSafe;
        }

        //[DataMember]
        //[XmlAttribute("bindMethod")]
        //public BindMethodEnum BindMethod;

        [DataMember]
        [XmlAttribute("resolveParameters")]
        public bool ResolveParameters { get; set; }

        [DataMember]
        [XmlAttribute("bindToHandle")]
        public bool BindToHandle { get; set; }

        [DataMember]
        [XmlAttribute("async")]
        public bool Async { get; set; }

        [DataMember]
        [XmlAttribute("target")]
        // This must be either a type name or a handle
        public string Target { get; set; }

        [DataMember]
        [XmlAttribute("engine")]
        // This must be the name of the engine to bind to
        public string Engine { get; set; }

        [DataMember]
        [XmlAttribute("methodName")]
        public string MethodName { get; set; }

        [DataMember]
        [XmlAttribute("methodOffset")]
        public int MethodOffset { get; set; }

        [DataMember]
        [XmlAttribute("functionName")]
        public string FunctionName { get; set; }

        [DataMember]
        [XmlAttribute("prefix")]
        public string Prefix { get; set; }

        [DataMember]
        [XmlAttribute("postfix")]
        public string PostFix { get; set; }

        [DataMember]
        [XmlAttribute("description")]
        public string Description { get; set; }

        [DataMember]
        [XmlAttribute("isExceptionSafe")]
        public bool IsExceptionSafe { get; set; }

        [XmlIgnore]
        public bool IsRemote { get; set; }

        [XmlIgnore]
        public bool IsStatic { get; set; }

        public string Name
        {
            get
            {
                return
                    (Prefix ?? "") +
                    (String.IsNullOrEmpty(FunctionName) ? MethodName : FunctionName) +
                    (String.IsNullOrEmpty(PostFix) ? "" : PostFix);
            }
        }

        public bool IsFastBind()
        {
            return !IsRemote               // Local
                && IsStatic                // Static
                && !ResolveParameters      // Direct
                && !BindToHandle           // Object
                && !Async;                 // Sync
        }

    }
}